#!/bin/bash

# Whisper.cpp 安装脚本
# 适用于 Ubuntu/Debian 和 macOS 系统

set -e

echo "=== 安装 Whisper.cpp 语音识别 ==="

# 检查系统
if [[ "$OSTYPE" == "darwin"* ]]; then
    echo "🍎 检测到 macOS 系统"
    SYSTEM="macos"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
    echo "🐧 检测到 Linux 系统"
    SYSTEM="linux"
else
    echo "❌ 不支持的操作系统: $OSTYPE"
    exit 1
fi

# 安装依赖
echo "📦 安装系统依赖..."

if [[ "$SYSTEM" == "macos" ]]; then
    # macOS 依赖安装
    if ! command -v brew &> /dev/null; then
        echo "❌ 请先安装 Homebrew: https://brew.sh/"
        exit 1
    fi
    
    echo "安装 macOS 依赖..."
    brew install cmake git wget python3
    
    # 检查 Xcode Command Line Tools
    if ! xcode-select -p &> /dev/null; then
        echo "📦 安装 Xcode Command Line Tools..."
        xcode-select --install
        echo "请等待 Xcode Command Line Tools 安装完成，然后重新运行此脚本"
        exit 1
    fi
else
    # Linux 依赖安装
    echo "安装 Linux 依赖..."
    sudo apt-get update
    sudo apt-get install -y \
        build-essential \
        cmake \
        git \
        wget \
        python3 \
        python3-pip
fi

# 克隆 Whisper.cpp 仓库
echo "📥 克隆 Whisper.cpp 仓库..."
cd /tmp
if [ -d "whisper.cpp" ]; then
    rm -rf whisper.cpp
fi

git clone https://github.com/ggerganov/whisper.cpp.git
cd whisper.cpp

# 编译 Whisper.cpp
echo "🔨 编译 Whisper.cpp..."
make

# 下载模型
echo "📥 下载 Whisper 模型..."
mkdir -p models
cd models

# 下载基础模型 (约 150MB)
echo "下载基础模型 (ggml-base.bin)..."
# wget -O ggml-base.bin https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.bin

# 下载中文优化模型 (可选)
echo "下载中文优化模型 (ggml-base-chinese.bin)..."
# wget -O ggml-base-chinese.bin https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base-chinese.bin

cd ..

# 安装到系统目录
echo "📁 安装到系统目录..."
if [[ "$SYSTEM" == "macos" ]]; then
    # macOS 安装路径
    INSTALL_DIR="/opt/whisper"
    sudo mkdir -p $INSTALL_DIR
    sudo cp build/bin/whisper-cli $INSTALL_DIR/whisper
    
    # 复制动态库文件
    echo "复制动态库文件..."
    sudo cp build/src/libwhisper.1.dylib $INSTALL_DIR/
    sudo cp build/src/libwhisper.dylib $INSTALL_DIR/
    sudo cp build/ggml/src/libggml.dylib $INSTALL_DIR/
    sudo cp build/ggml/src/libggml-base.dylib $INSTALL_DIR/
    sudo cp build/ggml/src/libggml-cpu.dylib $INSTALL_DIR/
    sudo cp build/ggml/src/ggml-blas/libggml-blas.dylib $INSTALL_DIR/
    sudo cp build/ggml/src/ggml-metal/libggml-metal.dylib $INSTALL_DIR/
    
    # 设置rpath
    echo "设置动态库路径..."
    sudo install_name_tool -add_rpath $INSTALL_DIR $INSTALL_DIR/whisper
    
    # 复制模型文件
    sudo cp -r models $INSTALL_DIR/
    
    # 设置权限
    sudo chmod +x $INSTALL_DIR/whisper
    
    # 创建软链接到 /usr/local/bin
    echo "🔗 创建软链接..."
    sudo ln -sf $INSTALL_DIR/whisper /usr/local/bin/whisper
else
    # Linux 安装路径
    INSTALL_DIR="/opt/whisper"
    sudo mkdir -p $INSTALL_DIR
    sudo cp build/bin/whisper-cli $INSTALL_DIR/whisper
    sudo cp -r models $INSTALL_DIR/
    
    # 复制动态库文件 (仅 Linux)
    sudo cp build/src/libwhisper.so* $INSTALL_DIR/
    sudo cp build/ggml/src/libggml*.so* $INSTALL_DIR/
    
    # 设置库路径 (仅 Linux)
    sudo sh -c 'echo "/opt/whisper" > /etc/ld.so.conf.d/whisper.conf'
    sudo ldconfig
    
    sudo chmod +x $INSTALL_DIR/whisper
    
    # 创建软链接
    echo "🔗 创建软链接..."
    sudo ln -sf $INSTALL_DIR/whisper /usr/local/bin/whisper
fi

# 测试安装
echo "🧪 测试安装..."
if $INSTALL_DIR/whisper --help > /dev/null 2>&1; then
    echo "✅ Whisper.cpp 安装成功!"
    echo "📁 安装路径: $INSTALL_DIR"
    echo "🔧 可执行文件: $INSTALL_DIR/whisper"
    echo "📦 模型路径: $INSTALL_DIR/models/"
    echo ""
    echo "🎯 使用方法:"
    echo "  whisper -m $INSTALL_DIR/models/ggml-base.bin -f audio.wav -l zh"
    echo ""
    echo "⚙️  环境变量配置:"
    echo "  export WHISPER_EXECUTABLE=$INSTALL_DIR/whisper"
    echo "  export WHISPER_MODEL_PATH=$INSTALL_DIR/models/ggml-base.bin"
else
    echo "❌ Whisper.cpp 安装失败!"
    exit 1
fi

# 清理临时文件
echo "🧹 清理临时文件..."
cd /
# rm -rf /tmp/whisper.cpp

echo "=== 安装完成 ===" 